
// Shortcuts
var log = YAHOO.log;

/**
 * @class
 * @constructor
 */
function Application() {
	
	var genefinder = null;
	
	var flyatlas = null;
	
	var deferredSelection = null;
	
	var that = this;
	
	// called by this.init
	
	this.initGenefinder = function() {
		
		// initialise genefinder widget

		log("instantiate a service for the genefinder widget");
		var service = new flyui.flybase.Service("../../../data/flybase");
		
		log("instantiate a renderer for the genefinder widget");
		var renderPane = document.getElementById("genefinderWidget");
		var renderer = new CustomGeneFinderRenderer(); // custom renderer
//		var renderer = new flyui.genefinder.DefaultRenderer(); // default renderer
		renderer.setCanvas(renderPane);
		
		log("instantiate a genefinder widget");
		genefinder = new flyui.genefinder.Widget(service, renderer);

	};
	
	// called by this.init
	
	this.initFlyatlas = function() {
		flyui.info("instantiate a service for the flyatlas widget");
		var service = new flyui.flyatlas.Service();
		service.setEndpoint("../../../data/flyatlas");
		
		flyui.info("instantiate a renderer for the flyatlas widget");
		var renderer = new flyui.flyatlas.Widget.DefaultRenderer(); 
		renderer.setCanvas($("#flyatlasWidget"));
		
		flyui.info("instantiate a flyatlas widget");
		flyatlas = new flyui.flyatlas.Widget(service, renderer);
		
	};
	
	// called by this.init
	
	this.initState = function() {
		var url = new String(window.location.href);
		var qi = url.indexOf("#query=");
		var si = url.indexOf(";selection=");
		
		if (qi > 0) {
			var end = (si > 0) ? si : url.length;
			var query = url.substring(qi+7,end);
			log("found query: "+query);
			genefinder.findGenesByAnyName(query,false);
		}
		
		if (si > 0) {
			var selection = url.substring(si+11,url.length);
			log("found selection: "+selection);
			deferredSelection = selection;
		}
		
	};

	this.init = function() {
		
		var logReader = new YAHOO.widget.LogReader("logger");

		this.initGenefinder();
		
		this.initFlyatlas();
		
		// attach gene selection listener to genefinder
		genefinder.subscribe("GENESELECTED", this.onGeneSelected, null);
		genefinder.subscribe("GENESFOUND", this.onGenesFound, null);

		// attach form submission listener to form
		log("hook form submission to widget call");
		YAHOO.util.Event.addListener("queryForm", "submit", this.onFormSubmitted);
		
		log("application loaded, showing main pane");
		flyui.mvcutils.hide(document.getElementById("loadingPane"));
		flyui.mvcutils.show(document.getElementById("applicationPane"));
		
		this.initState();
	};
	
	this.onGenesFound = function(type, args) {
		log("application: genes found");
		var genes = args[0];
		if (deferredSelection != null) {
			log("attempting to apply deferred selection: "+deferredSelection);
			var index = -1;
			for (var i=0; i<genes.length; i++) {
				if (deferredSelection == genes[i].flybaseID) {
					index = i;
				}
			}
			if (index >= 0) {
				genefinder.setSelectionIndex(index);
			}
			else {
				log("failed to apply deferred selection: "+deferredSelection, "error");
			}
			deferredSelection = null;
		}
	}
	
	this.onGeneSelected = function(type, args) {
		var gene = args[0];
		log("gene selected: "+gene+" "+gene.flybaseID);
		flyatlas.getAssaysByGene(gene);
		flyui.mvcutils.show(document.getElementById("flyatlasWidget"));
		that.setSelectionState(gene);
	};	
	
	this.onFormSubmitted = function(event) {
		log("form submitted");
		var query = document.getElementById("queryInput").value;
		document.getElementById("queryInput").value = "";
		var caseSensitive = false;
		log("case sensitive: "+caseSensitive);
		genefinder.findGenesByAnyName(query, caseSensitive);
		flyui.mvcutils.hide(document.getElementById("flyatlasWidget"));
		that.setQueryState(query);
	};
	
	this.setQueryState = function(query) {
		var url = new String(window.location.href);
		var term = url.indexOf("#");
		if (term > 0) {
			url = url.substring(0, term);
		}
		window.location.href = url + "#query=" + query;
	};
	
	this.setSelectionState = function(gene) {
		var url = new String(window.location.href);
		var term = url.indexOf(";selection=");
		if (term > 0) {
			url = url.substring(0, term);
		}
		window.location.href = url + ";selection=" + gene.flybaseID;
	}
	
	this.init();
}

// on dom ready, instantiate application
YAHOO.util.Event.onDOMReady(function() {new Application();});